<?php
namespace app\mail\controller;

use tp5_lib\common\controller\BaseController;
use tp5_lib\install\logic\DynamicConfig;
use PHPMailer\PHPMailer\PHPMailer;
use DataWidget\widget\FormWidget;

class IndexController extends BaseController
{
    protected $config=[
        ["code"=>"host","title"=>"域名/服务器地址","value"=>"smtp.qq.com"],
        ["code"=>"secure","title"=>"加密连接方式","value"=>"ssl","type"=>"select","options"=>"ssl,SSL;tls,TLS"],
        ["code"=>"port","title"=>"端口","value"=>"587","describe"=>"SMTP服务默认端口是25(用于中继),587(用于提交),465(使用SSL，IETF不认可)"],
        ["code"=>"username","title"=>"账户名","value"=>"2539937374@qq.com"],
        ["code"=>"password","title"=>"密码","value"=>"nqkqlhnfztphdijd"],
        ["code"=>"from","title"=>"发件人邮箱","value"=>"2539937374@qq.com"],
        ["code"=>"from_name","title"=>"发件人","value"=>"管理员"],
    ];
    protected $mail=[
        ["code"=>"to","title"=>"收件人邮箱","value"=>"1620428040@qq.com"],
        ["code"=>"subject","title"=>"主题","value"=>""],
        ["code"=>"content","title"=>"内容","value"=>"","type"=>"html"],
    ];
    public function index()
    {
        $config_widget=FormWidget::alloc()->setAction(url("set"),"提交")->loadFields($this->config);
        try{
            $config=DynamicConfig::alloc("config")->read("mail");
            $config_widget->setValues($config);
        }
        catch(\Exception $exce){}
        $config_form=$this->fetch("widget@base/form",["form"=>$config_widget->toArray()]);
        $this->assign("config_form",$config_form);

        $mail_widget=FormWidget::alloc()->setAction(url("send"),"发送")->loadFields($this->mail);
        $this->assign("mail_form",$this->fetch("widget@base/form",["form"=>$mail_widget->toArray()]));
        return $this->fetch();
    }
    /** 设置发件人账号和密码 */
    public function set()
    {
        $config=input();
        DynamicConfig::alloc("config")->write("mail",$config);
        $this->success("设置成功");
    }
    /** 发送邮件 */
    public function send()
    {
        try{
            $config=DynamicConfig::alloc("config")->read("mail");
        }
        catch(\Exception $exce){
            $config=FormWidget::alloc()->loadFields($this->config)->getValues();
        }
        $data=input();
        $mailer=new PHPMailer();
        $mailer->isSMTP();
        $mailer->Host = $config["host"];
        $mailer->SMTPSecure = $config["secure"];
        $mailer->Port = $config["port"];
        $mailer->SMTPDebug = 0; //用于debug PHPMailer信息
        $mailer->SMTPAuth = true;
        $mailer->Username = $config["username"]; 
        $mailer->Password = $config["password"];
        $mailer->From = $config["from"];
        $mailer->FromName = $config["from_name"];
        $mailer->AddAddress($data["to"]);
        $mailer->WordWrap = 50;
        $mailer->IsHTML(true);
        $mailer->CharSet="utf-8";
        $mailer->Subject =$data["subject"];
        $mailer->Body = $data["content"];
        if(!$mailer->Send()){
            header('Content-Type: text/plain');
            echo $mailer->ErrorInfo;
            exit;
        }
        $this->success("发送成功","index");
    }
}
